package com.uzhie.util;

import com.uzhie.jt808.util.DateUtil;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

/**
 * Created by lilongfei on 2018/7/16.
 */
public class Tools {

    /**利用MD5进行加密
     * @param str  待加密的字符串
     * @return  加密后的字符串
     * @throws NoSuchAlgorithmException  没有这种产生消息摘要的算法
     * @throws UnsupportedEncodingException
     */
    public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        //确定计算方法
        MessageDigest md5=MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        String newstr=base64en.encode(md5.digest(str.getBytes("utf-8")));
        return newstr;
    }

    /**
     * 通过传入参数结合时间戳生成加密字符串ID
     * @param vr1
     * @return
     */
    public static String encodeID(String... vr1){
        String ID = DateUtil.datetimeToString(new Date());
        for (String str :vr1){
            ID +="_"+str;
        }
        return ID;
    }
}
